/*
 * @copyright: Huang Ding
 * @Author: ding-cx
 * @Date: 2022-04-29 11:05:48
 * @LastEditors: ding-cx
 * @LastEditTime: 2022-04-30 09:37:47
 * @Description: file content
 */

import DingTalkBasePublicService from "./BasePublicService";
import { Dingtalk } from "../dingtalk";

export default class MessageService extends DingTalkBasePublicService {
	/** 基础发送工作消息消息封装 */
	async sendWorkMessage(data: Dingtalk.ov.IRequestMessageWorkNotice) {
		const url = await this.getURL(`https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2`);
		const method = "POST";
		const data1 = {
			...data,
		};
		const res = await this.curl<Dingtalk.ov.IResponseMessageWorkNotice>(url, {
			contentType: "json",
			dataType: "json",
			method,
			data: data1,
		});
		this.ovcheckError(url, res);
		return res.data;
	}

	/**
	 *单人，多人发送文本工作消息
	 *
	 * @param {string} userid 用户userid,可逗号隔开提供多个userid，但不超过100个
	 * @param {string} msg
	 * @memberof PlatformDingTalkBaseMessageService
	 */
	async sendTextWorkMessageToUser(userid: string, msg: string) {
		const data: Dingtalk.ov.IRequestMessageWorkNotice = {
			agent_id: this.config.agentId,
			userid_list: userid,
			msg: {
				msgtype: "text",
				text: {
					content: msg,
				},
			},
		};
		return await this.sendWorkMessage(data);
	}

	/**
	 *
	 *单人，多人发送markdown消息
	 *
	 * @param {string} userid 用户userid,可逗号隔开提供多个userid，但不超过100个
	 *
	 * @param {string} userid
	 * @param {{ title: string; text: string }} msg
	 * @return {*}
	 * @memberof PlatformDingTalkBaseMessageService
	 */
	async sendMarkdownWorkMessageToUser(userid: string, msg: { title: string; text: string }) {
		const config = this.config;
		const data: Dingtalk.ov.IRequestMessageWorkNotice = {
			agent_id: config.agentId,
			userid_list: userid,
			msg: {
				msgtype: "markdown",
				markdown: {
					title: msg.title,
					text: msg.text,
				},
			},
		};
		return await this.sendWorkMessage(data);
	}

	/**
	 *所有人发送文本工作消息
	 *
	 * @param {string} msg
	 * @memberof PlatformDingTalkBaseMessageService
	 */
	async sendTextWorkMessageToAllUser(msg: string) {
		const config = await this.config;
		const data: Dingtalk.ov.IRequestMessageWorkNotice = {
			agent_id: config.agentId,
			to_all_user: true,
			msg: {
				msgtype: "text",
				text: {
					content: msg,
				},
			},
		};
		return await this.sendWorkMessage(data);
	}

	/**
	 *
	 *所有人发送markdown工作消息
	 *
	 * @param {{ title: string; text: string }} msg
	 * @return {*}
	 * @memberof PlatformDingTalkBaseMessageService
	 */
	async sendMarkdownWorkMessageToAllUser(msg: { title: string; text: string }) {
		const config = await this.config;
		const data: Dingtalk.ov.IRequestMessageWorkNotice = {
			agent_id: config.agentId,
			to_all_user: true,
			msg: {
				msgtype: "markdown",
				markdown: {
					title: msg.title,
					text: msg.text,
				},
			},
		};
		return await this.sendWorkMessage(data);
	}
}
